
/**
 * <p>Title:  </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) xio.name 2006</p>
 * @author xio
 */
function GetProcess(wrapper, tableViewer, toolbar, doc, jbpmDoc) {
	//ShowAlertMessageDialog( Ajax );
    //this.base = Ajax;
    //this.base();
    this.wrapper = wrapper;
    this.toolbar = toolbar;
    this.tableViewer = tableViewer;
    
	//add by Ray li
	this.doc = doc;
	this.jbpmDoc= jbpmDoc;
	
	// get the process name
	var pd = GetElementByTagName(this.jbpmDoc,"process-definition");
	var name = pd.getAttribute("name");

    var editable = this.wrapper.getModel().isEditable();
    //var model = XiorkFlowModelConverter.convertXMLToModel(doc); //original api
    var model = XiorkFlowModelConverter.convertXMLToModel(doc, jbpmDoc);
    model.setName(name);
    model.setEditable(editable);
    this.wrapper.setModel(model);
    this.wrapper.setChanged(false);
    if (this.tableViewer) {
        this.tableViewer.setModel(model);
    }
    this.setButtonEnable(true);
}

//GetProcess.prototype = new Ajax();

GetProcess.prototype.setButtonEnable = function (b) {
    if (this.toolbar) {
        this.toolbar.setButtonEnable(b);
    }
};

/*
GetProcess.prototype.onReadyStateChange = function (httpRequest) {
    if (httpRequest.readyState == 4) {
        if (httpRequest.status == 200) {
            this.processXMLHttpRequest(httpRequest);
        } else {
        	//处理过程出现错误！
            ShowAlertMessageDialog("\u5904\u7406\u8fc7\u7a0b\u51fa\u73b0\u9519\u8bef\uff01");
            this.setButtonEnable(true);
        }
    }
};*/

GetProcess.prototype.processXMLHttpRequest = function (httpRequest) {
    //var doc = httpRequest.responseXML;
    if (!doc) {
    	//操作结束，未知服务器处理结果！
        alertMessage("操作结束，未知服务器处理结果！");
        this.setButtonEnable(true);
        return false;
    }

    //
    var responseNodes = doc.getElementsByTagName("Response");
    if ((responseNodes != null) && (responseNodes.length > 0)) {
        var responseNode = responseNodes[0];
        var statusValue = eval(responseNode.getAttribute("status"));
        var alertStr = "";
        switch (statusValue) {
          case XiorkFlowWorkSpace.STATUS_FAIL:
      		//获取失败。
            alertStr = "获取失败。";
            break;
          case XiorkFlowWorkSpace.STATUS_XML_PARSER_ERROR:
      		//获取失败，xml解析出错。
            alertStr = "获取失败，xml解析出错。";
            break;
          case XiorkFlowWorkSpace.STATUS_IO_ERROR:
      		//获取失败，IO错误。
            alertStr = "获取失败，IO错误。";
            break;
          case XiorkFlowWorkSpace.STATUS_FILE_NOT_FOUND:
      		//获取失败，文件未找到。
            alertStr = "获取失败，文件未找到。";
            break;
          default:
        	//获取失败，未知错误。
            alertStr = "获取失败，未知错误。";
            break;
        }
        this.setButtonEnable(true);
        alertMessage(alertStr);
    } else {

		//debug convert jbpmxml to workflow mode >>>  
		//to be done for Agent
		//replace GetProcessDefinitionXML(String, long) here
		var jbpmDoc = new ActiveXObject("Msxml2.DOMDocument");
        jbpmDoc.loadXML("<process-definition name=\"dd\" xmlns=\"urn:jbpm.org:jpdl-3.1\"><start-state name=\"StartNode\"><transition name=\"\" to=\"Node\"/></start-state><end-state name=\"EndNode\"/><node name=\"Node\"><action class=\"com.ctit.test.class\"><nodevar1>nodevar1value1</nodevar1><nodevar2>nodevar1value2</nodevar2></action><transition name=\"\" to=\"TaskNode\"/></node><task-node name=\"TaskNode\"><task name=\"task_test\"><controller><variable name=\"tasknodevar1\" access=\",,\"/><variable name=\"tasknodevar2\" access=\",write,\"/><variable name=\"tasknodevar3\" access=\"read,,required\"/></controller></task><transition name=\"\" to=\"EndNode\"/></task-node></process-definition>");
		//ShowAlertMessageDialog(jbpmDoc.xml);
		//<<<
        var editable = this.wrapper.getModel().isEditable();
        //var model = XiorkFlowModelConverter.convertXMLToModel(doc); //original api
        var model = XiorkFlowModelConverter.convertXMLToModel(doc, jbpmDoc);
        model.setName(this.name);
        model.setEditable(editable);
        this.wrapper.setModel(model);
        this.wrapper.setChanged(false);
        if (this.tableViewer) {
            this.tableViewer.setModel(model);
        }
        this.setButtonEnable(true);
    }
};

function GetElementByTagName(node, name)
{
	if(name.indexOf(":")<0||window.ActiveXObject)
	{
		var elems = node.getElementsByTagName(name);
		if(elems&&elems.length>0)
			return elems[0];	
	}
	else
	{
		var elems = node.getElementsByTagNameNS("http://www.reallysi.com",name.substring(name.indexOf(":")+1,name.length));
		if(elems&&elems.length>0)
			return elems[0];	
	}
}